#ifndef XYD_MANAGER_H
#define XYD_MANAGER_H


#define DEVICE_PORT "DEVICE_PORT"
#define DEVICE_STATUS_DEBUG "DEVICE_STATUS_DEBUG"
#define WAN_IP_ADDRESS "WAN_IP_ADDRESS"
#define SSID_NAME "SSID_NAME"
#define DEVICE_ID "DEVICE_ID"
#define RTSP_IP "RTSP_IP"

#include <memory>
#include <string>
#include <thread>
#include <mavsdk/plugins/action/action_impl.h>
#include <mavsdk/plugins/manual_control/manual_control_impl.h>
#include <mavsdk/plugins/telemetry/telemetry.h>
#include <mavsdk/plugins/mission/mission.h>
using namespace mavsdk;

namespace XydAgent
{
    //设备状态信息(0离线1在线2忙碌3空闲4故障)
    enum class DeviceStatusTbl
    {
        DEVICE_STATE_OFFLINE = 0,
        DEVICE_STATE_NORMAL,
        DEVICE_STATE_BUSY,
        DEVICE_STATE_IDLE,
        DEVICE_STATE_FAULT,
    };

    enum class CommonFlightMode
    {
        //0x00 手动 0x01 自动悬停 0x02 自动导航 0x03 航线准备 0x04 自动起飞 0x05 自动降落 0x06 返航降落
        //0x07 返航拔高 0x08 飞机锁定 0x09 紧急悬停 0x0A 起飞准备 0x0B 辅助飞行 0x0C 自动调整 0x0D 引导模式
        //0x0E 任务模式 0x0F 返航 0x10 offboard模式 0x11 跟随模式 0x12 定高模式 0x13 定点模式 0x14 Acro特技模式
        //0x15 Rattitude角速率模式 0x16 自稳模式 0xff 未知模式
        Manual,
        Hold,
        AutoNav,
        NavDataPrepare,
        AutoTakeoff,
        AutoLand,
        ReturnLand,
        ReturnSoar,
        DisArm,
        UrgentHold,
        TakeoffPrepare,
        FBWA,
        Autotune,
        Guided,
        Mission,
        ReturnToLaunch,
        Offboard,
        FollowMe,
        Altctl,
        Posctl,
        Acro,
        Rattitude,
        Stabilized,
        Unknown = 0xFF,
    };

    struct JoyCache
    {
        float xyd_pitch = 0;
        float xyd_roll = 0;
        float xyd_yaw = 0;
        float xyd_throttle = 0.5;
    };

    class XydManager
    {
    public:
        ~XydManager();

        static XydManager* getInstance();
        static float scaleJoyValue(int inputValue, int outputMin, int outputMax);
        static Mission::MissionItem make_mission_item(double latitude_deg, double longitude_deg, float relative_altitude_m, float speed_m_s, bool is_fly_through, float
                                                      loiter_time_s);
        static void armd_callback(bool armd);
        static void battery_status_callback(Telemetry::Battery battery);
        static void position_callback(Telemetry::Position position);
        static void attitude_angular_velocity_callback(Telemetry::AngularVelocityBody angularVelocityBody);
        static void attitude_euler_callback(Telemetry::EulerAngle euler_angle);
        static void home_position_callback(Telemetry::Position position);
        static void position_velocity_ned_callback(Telemetry::PositionVelocityNed velocityNed);
        static void gps_info_callback(Telemetry::GpsInfo gpsInfo);
        static void flight_mode_callback(Telemetry::FlightMode flightMode);
        static void health_status_callback(Telemetry::Health health);
        static void health_all_callback(bool is_all_ok);
        static CommonFlightMode fly_mode_transform(const Telemetry::FlightMode& flightMode);
        bool init();
        void shutdown();
        void set_device_status(DeviceStatusTbl status);
        JoyCache joy_cache;

        std::shared_ptr<ActionImpl> m_xyd_action;
        std::shared_ptr<ManualControlImpl> m_xyd_manual_control;
        std::shared_ptr<Mission> m_xyd_mission;
        std::shared_ptr<TelemetryImpl> m_telemetry;


    private:
        XydManager();
        // 成员变量管理 Telemetry 插件和后台线程
        std::thread m_telemetryThread;
        std::atomic<bool> m_stopTelemetry{false};

        std::thread m_default_manual_thread;

        void telemetry_callback_loop();
        void default_manual_control_loop();
    };
}

#endif //XYD_MANAGER_H
